package com.bcbim.api_web.sys.controller;

import com.bcbim.api_web.sys.init.code.CodeMap;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Code;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.MyCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Description: 系统码值controller
 * @Author haoshuai
 * @Date 2020/9/22
 * @Version V1.0
 **/
@Slf4j
@RestController
@RequestMapping("/sysCode")
public class SysCodeController {

    /**
     * @Description: 获取码值下拉列表
     * @Param: type 码值类型
     * @Return: ReturnMsg
     * @Author: haoshuai
     * @Date: 2020/9/22
     */
    @RequestMapping(value = "/getSysCode", method = RequestMethod.POST)
    public ReturnMsg getSysCode(@RequestParam("type") String type) {

        //参数校验
        try {
            checkParamsForGetSysCode(type);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }

        String codeType = CodeMap.CODE_MAPPING.get(type);

        if (StringUtils.isEmpty(codeType)) {
            return ReturnMsg.consFail(type+"暂无对应码值");
        }

        String mapName = codeType.replace(Constant.TYPE_SUFFIX, Constant.MAP_SUFFIX);
        try {
            Map<String, String> codeMap =
                    (Map<String, String>) CodeMap.class.getField(mapName).get(null);
            List<Code> list = MyCodeUtil.sysCodeMapToList(codeMap);
            return ReturnMsg.consSuccess(list);
        } catch (IllegalAccessException e) {
            return ReturnMsg.consFail("获取码值异常");
        } catch (NoSuchFieldException e) {
            return ReturnMsg.consFail("获取码值异常");
        }

    }

    /**
     * @Description: getSysCode方法参数校验
     * @Param: type 码值类型
     * @Author: haoshuai
     * @Date: 2020/9/22
     */
    private void checkParamsForGetSysCode(String type) throws ParamException {
        if (StringUtils.isEmpty(type)) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 获取码值下拉列表, 含有"全部"码值
     * @Param: type 码值类型
     * @Return: ReturnMsg
     * @Author: haoshuai
     * @Date: 2020/9/22
     */
    @RequestMapping(value = "/getSysCodeAddAll", method = RequestMethod.POST)
    public ReturnMsg getSysCodeAddAll(@RequestParam("type") String type) {

        //参数校验
        try {
            checkParamsForGetSysCode(type);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }

        String codeType = CodeMap.CODE_MAPPING.get(type);

        if (StringUtils.isEmpty(codeType)) {
            return ReturnMsg.consFail(type+"暂无对应码值");
        }

        String mapName = codeType.replace(Constant.TYPE_SUFFIX, Constant.MAP_SUFFIX);
        try {
            Map<String, String> codeMap =
                    (Map<String, String>) CodeMap.class.getField(mapName).get(null);
            List<Code> list = MyCodeUtil.addAllCodeForMap(codeMap);
            return ReturnMsg.consSuccess(list);
        } catch (IllegalAccessException e) {
            return ReturnMsg.consFail("获取码值异常");
        } catch (NoSuchFieldException e) {
            return ReturnMsg.consFail("获取码值异常");
        }

    }

}
